package com.techstar.mqttprocesser.service.mqtt.handler;

import com.alibaba.fastjson.JSON;
import com.techstar.mqttprocesser.service.mqtt.mapper.MqttJsonDataModelMapper;
import com.techstar.mqttprocesser.service.mqtt.model.DeviceType;
import com.techstar.mqttprocesser.service.mqtt.model.MqttJsonDataModel;
import com.techstar.mqttprocesser.service.mqtt.utils.TimeUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

/**
 * @author lilei
 * @create 2025-04-07-2025/4/7
 */
@Service
@Slf4j
public class DataCleanService {

    @Autowired
    private MqttJsonDataModelMapper mqttJsonDataModelMapper;

    //每隔一小时清理一次数据
    @Scheduled(cron = "0 * * * * ?")
    public void cleanData() {
        try {
            MqttJsonDataModel dataModel = new MqttJsonDataModel();
            //只保留30天的数据
            dataModel.setDataTime(TimeUtils.formatDateTime(System.currentTimeMillis() - 30L * 24 * 60 * 60 * 1000));
            dataModel.setDeviceName(DeviceType.luzhou.getCode());

            //清理数据
            log.info("开始清理数据,dataModel:{}", JSON.toJSONString(dataModel));
            mqttJsonDataModelMapper.cleanData(dataModel);
        }catch (Exception e){
            log.error("清理数据失败", e);
        }
    }

}
